package com.fingard.dsp.bank.directbank.zyb01;


import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.*;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.net.ConnState;
import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class ZYB01SinglePay extends ZYB01Base implements ISinglePay {

	//单笔支付，不需采用流处理方式
	public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception{
		//返回ATS的对象
		RetAtsSingle retAts = new RetAtsSingle(p_req1908.reqHeader);
		RetAtsSglDetail tmpRetDetail;
		tmpRetDetail = externalPayments(p_req1908);
		if(tmpRetDetail.respCode.equals("-1") && StringHelper.isNullOrEmpty(tmpRetDetail.reqSeqID)){
			tmpRetDetail.reqSeqID = p_req1908.singleDetail.askNumber;
		}
		retAts.writeRetDetail(tmpRetDetail);
		return retAts;
	}

	@Override
	public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
		return null;
	}

	//190802对外支付
	private RetAtsSglDetail externalPayments(ReqSingle p_req1908) throws Exception{
		RetAtsSglDetail retDetail = new RetAtsSglDetail();
		ZYB01Header bankHeader = new ZYB01Header();
		if(p_req1908.singleDetail.sameBnk.equals("1")) {//同行
			bankInnerTransfer(p_req1908, retDetail, bankHeader);
		}
        else{//跨行
			crossBankTransfer(p_req1908,retDetail,bankHeader);
		}
		return retDetail;
	}
	//同行支付
	private RetAtsSglDetail bankInnerTransfer(ReqSingle p_req1908,RetAtsSglDetail retDetail,ZYB01Header bankHeader) throws Exception{
		String sigData = "1";
		String payeeType = "I";
		String charsetName = "UTF-8";
		bankHeader.trsCode = "01B2EBankInnerTransfer";
		XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
		//写入报文头部信息
		writeCommonHeader(xmlWriter, bankHeader,reqHeader.reqSeqID);
		/*<Body>
		<PayerAcNo>8015014000000004444</PayerAcNo>
		<PayerAcName></PayerAcName>
		<PayeeAcNo>509010670308019</PayeeAcNo>
		<PayeeAcName>פ</PayeeAcName>
		<Amount>100000.00</Amount>
		<Currency>CNY</Currency>
		<Remark></Remark>
		<PayeeType>I</PayeeType>
		</Body>
		</Message>*/
		XElement tmpXElement = new XElement("Body","");
		tmpXElement.addChild(new XElement("PayerAcNo",p_req1908.singleDetail.payAct));
		tmpXElement.addChild(new XElement("PayerAcName",p_req1908.singleDetail.payName));
		tmpXElement.addChild(new XElement("PayeeAcNo",p_req1908.singleDetail.recAct));
		tmpXElement.addChild(new XElement("PayeeAcName",p_req1908.singleDetail.recName));
		tmpXElement.addChild(new XElement("Amount",p_req1908.singleDetail.payAmount));
		tmpXElement.addChild(new XElement("Currency",convertCurToBank(p_req1908.singleDetail.payCur)));
		tmpXElement.addChild(new XElement("Remark",p_req1908.singleDetail.purpose));
		if(p_req1908.singleDetail.isPrivate.equals("1")){
			payeeType = "P";
		}
		else{
			payeeType = "I";
		}
		tmpXElement.addChild(new XElement("PayeeType",payeeType));
		xmlWriter.writeXElement(tmpXElement);
		xmlWriter.writeEndElement();
		xmlWriter.writeEndDocument();//报文结束
		String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(),bankHeader,sigData,charsetName);//发送给银行
		if(tmpStrRet[0].length() == 0){
			StringBuilder tmpSbMsg = new StringBuilder();
			//解析银行返回
			XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
			if(tmpBkReader != null){
				retDetail.respCode = "0";
				retDetail.respInfo = tmpBkReader.getFirstText("/Reply_Msg/Sys_Head/ReturnMsg");
				retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
				retDetail.transState = tmpBkReader.getFirstText("/Reply_Msg/Sys_Head/ReturnCode");
				retDetail.payInfoCode = retDetail.transState;
			}
			else{
				retDetail.respCode = "-1";
				retDetail.respInfo = tmpSbMsg.toString();
			}
		}
		else if(tmpStrRet[0].equals(ConnState.FAIL)){
			retDetail.respCode = "0";
			retDetail.respInfo = tmpStrRet[1];
			retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
			retDetail.transState = PaymentState.FAIL_Payment;
			retDetail.payInfoCode = tmpStrRet[0];
		}
		else{
			retDetail.respCode = "-1";
			retDetail.respInfo = tmpStrRet[1];
		}
		return retDetail;
	}
	//跨行支付
	private RetAtsSglDetail crossBankTransfer(ReqSingle p_req1908,RetAtsSglDetail retDetail,ZYB01Header bankHeader) throws Exception{
		String sigData = "1";
		String charsetName = "UTF-8";
		bankHeader.trsCode = "01B2ECrossBankTransfer";
		XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
		writeCommonHeader(xmlWriter, bankHeader,reqHeader.reqSeqID);
		/*<Body>
		<PayerAcNo>521012735403012</PayerAcNo>
		<PayerAcName>˾</PayerAcName>
		<PayeeAcNo>0402024409300161221</PayeeAcNo>
		<PayeeAcName>800003208</PayeeAcName>
		<Amount>83.01</Amount>
		<TrsMode>D</TrsMode>
		<Currency>CNY</Currency>
		<Remark></Remark>
		<PayeeBankName></PayeeBankName>
		<PayeeDeptName></PayeeDeptName>
		<PayeeDeptId>102121000391</PayeeDeptId>
		</Body>
		</Message>*/
		XElement tmpXElement = new XElement("Body","");
		tmpXElement.addChild(new XElement("PayerAcNo",p_req1908.singleDetail.payAct));
		tmpXElement.addChild(new XElement("PayerAcName",p_req1908.singleDetail.payName));
		tmpXElement.addChild(new XElement("PayeeAcNo",p_req1908.singleDetail.recAct));
		tmpXElement.addChild(new XElement("PayeeAcName",p_req1908.singleDetail.recName));
		tmpXElement.addChild(new XElement("Amount",p_req1908.singleDetail.payAmount));
		String trsMode = "D";// C收入 D支出
		if(p_req1908.singleDetail.isLargeAmt()){
			trsMode = "C";
		}
		else if(p_req1908.singleDetail.paySpd.equals("1")){//加急走大额
			trsMode = "C";
		}
		else{
			trsMode = "D";
		}
		tmpXElement.addChild(new XElement("TrsMode",trsMode));
		tmpXElement.addChild(new XElement("Currency",convertCurToBank(p_req1908.singleDetail.payCur)));
		tmpXElement.addChild(new XElement("Remark",p_req1908.singleDetail.purpose));
		tmpXElement.addChild(new XElement("PayeeBankName",p_req1908.singleDetail.recBankName));
		tmpXElement.addChild(new XElement("PayeeDeptName",p_req1908.singleDetail.recBankLocation));
		tmpXElement.addChild(new XElement("PayeeDeptId",p_req1908.singleDetail.cnapsCode));
		xmlWriter.writeXElement(tmpXElement);
		xmlWriter.writeEndElement();
		xmlWriter.writeEndDocument();//写入报文完成
		String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(),bankHeader,sigData,charsetName);//发送银行
		if(tmpStrRet[0].length() == 0){
			StringBuilder tmpSbMsg = new StringBuilder();
			//解析银行返回
			XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
			if(tmpBkReader != null){
				retDetail.respCode = "0";
				retDetail.respInfo = tmpBkReader.getFirstText("/Reply_Msg/Sys_Head/ReturnMsg");
				retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
				retDetail.transState = tmpBkReader.getFirstText("/Reply_Msg/Sys_Head/ReturnCode");
				retDetail.payInfoCode = retDetail.transState;
			}
			else{
				retDetail.respCode = "-1";
				retDetail.respInfo = tmpSbMsg.toString();
			}
		}
		else if(tmpStrRet[0].equals(ConnState.FAIL)){
			retDetail.respCode = "0";
			retDetail.respInfo = tmpStrRet[1];
			retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
			retDetail.transState = PaymentState.FAIL_Payment;
			retDetail.payInfoCode = tmpStrRet[0];
		}
		else{
			retDetail.respCode = "-1";
			retDetail.respInfo = tmpStrRet[1];
		}
		return retDetail;
	}
	//1944 状态查询
	public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception{
		//返回ATS的对象
		RetAtsSingle retAts = new RetAtsSingle(p_req1944.reqHeader);
		
		RetAtsSglDetail retDetail = new RetAtsSglDetail();
		ZYB01Header bankHeader = new ZYB01Header();
		bankHeader.trsCode = "05B2EPayResultQry";
		String sigData = "0";
		String charsetName = "GBK";
		XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
		writeCommonHeader(xmlWriter, bankHeader,reqHeader.reqSeqID);//写入通用的头部节点
		xmlWriter.writeStartElement("Body");
		xmlWriter.writeElementString("FlowNo",reqHeader.reqSeqID);
		xmlWriter.writeEndElement();
		xmlWriter.writeEndDocument();
		try{
			String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(),bankHeader,sigData,charsetName);
			if(tmpStrRet[0].length() == 0){
				StringBuilder tmpSbMsg = new StringBuilder();
				XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
				if(tmpBkReader != null){
					String returnCode = tmpBkReader.getFirstText("/Reply_Msg/Sys_Head/ReturnCode");
					String returnMsg = tmpBkReader.getFirstText("/Reply_Msg/Sys_Head/ReturnMsg");
					if(returnCode != null && isActiveRetCode(returnCode)){
								retDetail.reqSeqID = p_req1944.askNumber;
								retDetail.respCode = "0";
								retDetail.respInfo = returnMsg;
								retDetail.payInfoCode = returnCode;
						        retDetail.transState = returnCode;
					}
					else{
						retDetail.respCode = "-1";
						retDetail.respInfo = returnMsg;
					}
				}
				else{
					retDetail.respCode = "-1";
					retDetail.respInfo = tmpSbMsg.toString();
				}
			}
			else{
				retDetail.respCode = "-1";
				retDetail.respInfo = tmpStrRet[1];
			}
		}
		catch (Exception e) {
			retDetail.respCode = "-1";
			retDetail.respInfo = e.getMessage();
			e.printStackTrace();
		}
		
		retAts.writeRetDetail(retDetail);
		return retAts;
	}

	@Override
	public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
		return null;
	}
}
